// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   REALImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.QTY;
import com.sunwayhorizo.health.hl7.datatypes.REAL;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

import java.math.BigDecimal;

import java.text.StringCharacterIterator;

import java.util.StringTokenizer;


// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            ANYImpl, BLImpl, CSImpl, INTImpl, 
//            NullFlavorImpl, STImpl, TYPEImpl

public class REALImpl extends ANYImpl
    implements REAL
{

    public static final String RCS_ID = "$Header: REALImpl.java 115.33.2050.3 2005/12/20 07:34:20 amchanda noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: REALImpl.java 115.33.2050.3 2005/12/20 07:34:20 amchanda noship $", "oracle.apps.ctb.hl7.types.common");
    private String mValue;
    private static final REAL nullNI = new REALImpl(CSImpl.valueOf("NI"));
    private static final REAL nullOTH = new REALImpl(CSImpl.valueOf("OTH"));
    private static final REAL nullNINF = new REALImpl(CSImpl.valueOf("NINF"));
    private static final REAL nullPINF = new REALImpl(CSImpl.valueOf("PINF"));
    private static final REAL nullUNK = new REALImpl(CSImpl.valueOf("UNK"));
    private static final REAL nullASKU = new REALImpl(CSImpl.valueOf("ASKU"));
    private static final REAL nullNAV = new REALImpl(CSImpl.valueOf("NAV"));
    private static final REAL nullMSK = new REALImpl(CSImpl.valueOf("MSK"));
    private static final REAL nullNA = new REALImpl(CSImpl.valueOf("NA"));
    private static final REAL nullNASK = new REALImpl(CSImpl.valueOf("NASK"));

    protected REALImpl(String s)
    {
        if(s == null)
        {
            throw ANYImpl.newNullException("REAL", "constructor", "value");
        } else
        {
            mValue = s;
            return;
        }
    }

    protected REALImpl(CS cs)
    {
        super(true, cs);
        mValue = null;
    }

    public static REAL valueOf(String s)
    {
        return new REALImpl(s);
    }

    public static REAL nullREAL()
    {
        return nullREAL(null);
    }

    public BL equal(ANY any)
    {
        if(!(any instanceof REAL))
            return BLImpl.FALSE;
        if(isNull().or(any.isNull()).isTrue())
        {
            return BLImpl.FALSE;
        } else
        {
            REAL real = (REAL)any;
            return BLImpl.valueOf(mValue.equals(real.stringValue()));
        }
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.REAL;
    }

    public ST literal()
    {
        return STImpl.valueOf(mValue);
    }

    public BL lessOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL", "lessOrEqual");
    }

    public BL compares(QTY qty)
    {
        BLImpl blimpl = null;
        if(qty == null || isNull().or(qty.isNull()).isTrue())
            blimpl = BLImpl.FALSE;
        else
        if(qty.dataType().equal(com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.REAL).isTrue())
            blimpl = BLImpl.TRUE;
        else
            blimpl = BLImpl.FALSE;
        return blimpl;
    }

    public TYPE diffType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.REAL;
    }

    public QTY minus(QTY qty)
    {
        REAL real;
        if(isNull().or(compares(qty).not()).isTrue())
        {
            real = nullREAL(NullFlavorImpl.NA);
        } else
        {
            REAL real1 = (REAL)qty;
            BigDecimal bigdecimal = bigDecimalValue().subtract(real1.bigDecimalValue());
            real = valueOf(bigdecimal.toString());
        }
        return real;
    }

    public QTY plus(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL", "plus");
    }

    public BL isZero()
    {
        throw ANYImpl.newUnsupportedException("REAL", "isZero");
    }

    public BL lessThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL", "lessThan");
    }

    public BL greaterOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL", "greaterOrEqual");
    }

    public BL greaterThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("REAL", "greaterThan");
    }

    public REAL negated()
    {
        throw ANYImpl.newUnsupportedException("REAL", "negated");
    }

    public REAL times(REAL real)
    {
        throw ANYImpl.newUnsupportedException("REAL", "times");
    }

    public REAL inverted()
    {
        throw ANYImpl.newUnsupportedException("REAL", "inverted");
    }

    public INT toINT()
    {
        if(isNull().isTrue())
            return INTImpl.nullINT(nullFlavor());
        else
            return INTImpl.valueOf((long)doubleValue());
    }

    public REAL power(REAL real)
    {
        throw ANYImpl.newUnsupportedException("REAL", "power");
    }

    public INT precision()
    {
        boolean flag = false;
        INT int1 = null;
        if(nonNull().booleanValue())
        {
            String s = null;
            if(doubleValue() < 0.0D)
                s = mValue.substring(1);
            else
                s = mValue;
            StringTokenizer stringtokenizer = new StringTokenizer(s.toUpperCase(), "E");
            String s1 = stringtokenizer.nextToken();
            stringtokenizer = new StringTokenizer(s1, ".");
            String s2 = stringtokenizer.nextToken();
            int i = countSigDigits(s2);
            if(stringtokenizer.hasMoreTokens())
            {
                String s3 = stringtokenizer.nextToken();
                if(i > 0)
                {
                    i += s3.length();
                } else
                {
                    i += countSigDigits(s3);
                    if(i == 0)
                        i = s3.length() + 1;
                }
            }
            if(i == 0)
                i++;
            int1 = INTImpl.valueOf(i);
        } else
        {
            int1 = INTImpl.nullINT();
        }
        return int1;
    }

    private int countSigDigits(String s)
    {
        int i = 0;
        StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
        for(char c = stringcharacteriterator.first(); c != '\uFFFF' && Character.isDigit(c); c = stringcharacteriterator.next())
        {
            if(c == '0' && i > 0)
                i++;
            if(c != '0')
                i++;
        }

        return i;
    }

    public BL isOne()
    {
        throw ANYImpl.newUnsupportedException("REAL", "isOne");
    }

    public double doubleValue()
    {
        return Double.valueOf((mValue == null || mValue.equals("") ? "0" : mValue)).doubleValue();
    }

    public String stringValue()
    {
        return mValue;
    }

    public BigDecimal bigDecimalValue()
    {
        if(isNull().isTrue())
            return null;
        else
            return new BigDecimal(mValue);
    }

    public String toString()
    {
        if(isNull().isTrue())
            return "<Null:" + nullFlavor().toString() + ">";
        else
            return mValue;
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
            i = 37 * i + mValue.hashCode();
        return i;
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
//            xmlelement.setAttribute("value", stringValue());
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    public static REAL nullREAL(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

}
